package com.ddxcloud.movie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddxcloud.common.handler.BusinessException;
import com.ddxcloud.movie.entity.Channel;
import com.ddxcloud.movie.mapper.ChannelMapper;
import com.ddxcloud.movie.service.ChannelService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements ChannelService {

    @Override
    public Channel getChannel() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        if (null == request.getHeader("ChannelId") || request.getHeader("ChannelId").isEmpty()) {
            throw new BusinessException("请在请求头上传递渠道ID");
        }
        return baseMapper.selectById(request.getHeader("ChannelId"));
    }
}
